﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Models;
using DbUtility;
using Web.Areas.Admin.Lib;
using Common;

namespace Web.Areas.Admin.Controllers
{
    public class PlayerController : BaseController
    {
        public ActionResult Index(PlayerSearch search)
        {
            playerLogic pl = new playerLogic();
            var list = pl.Getplayers(new player(), PageIndex, PageSize, search.GenerateWhere(), "sort", "asc");
            if (Request.IsAjaxRequest())
            {
                return PartialView("_Index", list);
            }
            return View(list);
        }

        public ActionResult Create()
        {
            DBContext db = new DBContext();
            player player = new player();
            player.sort = (int)db.ExecuteDataTableSql("select ISNULL(MAX(sort),0) from player").Rows[0][0] + 1;
            return View(player);
        }
        [HttpPost]
        public ActionResult Create(player player)
        {
            try
            {
                playerLogic pl = new playerLogic();
                player.createdate = DateTime.Now;
                player.createuserid = Account.ID;
                var result = pl.Create(player);
                if (result > 0)
                {
                    return SuccessMsg("创建成功！");
                }
                else
                {
                    return ErrorMsg("创建失败，网络错误！");
                }
            }
            catch (Exception e)
            {
                return ErrorMsg(e.Message);
            }
        }

        public ActionResult Edit(int id)
        {
            playerLogic pl = new playerLogic();
            var dbPlayer = pl.Getplayer(id);
            return View(dbPlayer);
        }
        [HttpPost]
        public ActionResult Edit(player player)
        {
            try
            {
                playerLogic pl = new playerLogic();
                player.modifydate = DateTime.Now;
                player.modifyuserid = Account.ID;
                var result = pl.Update(player);
                if (result)
                {
                    return SuccessMsg("编辑成功！", player.id);
                }
                else
                {
                    return ErrorMsg("编辑失败，网络错误！");
                }
            }
            catch (Exception e)
            {
                return ErrorMsg(e.Message);
            }
        }
        [HttpPost]

        public ActionResult Delete(int id)
        {
            try
            {
                playerLogic pl = new playerLogic();
                var dbPlayer = pl.Getplayer(id);
                if (dbPlayer == null)
                {
                    return ErrorMsg("参赛者不存在或已删除！");
                }
                var result = pl.Delete(dbPlayer);
                if (result)
                {
                    return SuccessMsg("删除成功！");
                }
                else
                {
                    return ErrorMsg("删除败，网络错误！");
                }
            }
            catch (Exception e)
            {
                return ErrorMsg(e.Message);
            }
        }
        [HttpPost]
        public ActionResult OpenOrClose(int id, bool isOpen)
        {
            try
            {
                DBContext db = new DBContext();
                playerLogic pl = new playerLogic();
                var dbPlayer = pl.Getplayer(id);
                if (dbPlayer == null)
                {
                    return ErrorMsg("参赛者不存在或已删除！");
                }
                dbPlayer.isopen = !isOpen;
                var result = pl.Update(dbPlayer);
                if (result)
                {
                    if (!isOpen)
                    {
                        var r = db.ExecuteNonQuerySql(string.Format("update player set isopen = 0 where id !={0} and isopen = 1", dbPlayer.id));
                    }
                    return SuccessMsg((isOpen ? "评分已关闭！" : "评分已开启！") + "成功！");
                }
                else
                {
                    return ErrorMsg("删除败，网络错误！");
                }
            }
            catch (Exception e)
            {
                return ErrorMsg(e.Message);
            }

        }

        [HttpGet]
        public ActionResult ExportAll()
        {
            try
            {
                DBContext db = new DBContext();
                var dataTable = db.ExecuteDataTableSql("select * from player where isdeleted=@isdeleted order by sort", new List<System.Data.Common.DbParameter>()
                {
                    new System.Data.SqlClient.SqlParameter("@isdeleted",false){SqlDbType=System.Data.SqlDbType.Bit},
                });
                var dic = new Dictionary<string, string>
                {
                    {"sort", "选手编号"},
                    {"name", "选手姓名"},
                    {"average", "平均分"},
                };
                ExcelHelper.DataTableToExcel(dataTable, dic, "选手平均分列表" + DateTime.Now.Format("yyyyMMddHHmmss"));
                return Content("");
            }
            catch (Exception ex)
            {
                return ErrorMsg(ex.Message);
            }
        }
        [HttpGet]
        public ActionResult ExportByPlayer(int id)
        {
            try
            {
                DBContext db = new DBContext();
                var dbPlayer = new playerLogic().Getplayer(id) ?? new player();

                var dataTable = db.ExecuteDataTableSql("select * from user_scorelog where isdeleted=@isdeleted and playerid=@playerid order by score desc", new List<System.Data.Common.DbParameter>()
                {
                    new System.Data.SqlClient.SqlParameter("@isdeleted",false){SqlDbType=System.Data.SqlDbType.Bit},
                    new System.Data.SqlClient.SqlParameter("@playerid",id){SqlDbType=System.Data.SqlDbType.Int},

                });
                var dic = new Dictionary<string, string>
                {
                    {"nickname", "评分人昵称"},
                    {"mobile", "评分人手机"},
                    {"score", "分数"},
                    {"scoretypestr", "打分类型"},
                    {"createdate", "评分时间"},
                };
                ExcelHelper.DataTableToExcel(dataTable, dic, dbPlayer.id + "号选手-" + dbPlayer.name + "得分详情" + DateTime.Now.Format("yyyyMMddHHmmss"));
                return Content("");
            }
            catch (Exception ex)
            {
                return ErrorMsg(ex.Message);
            }
        }

        [HttpPost]
        public ActionResult ClearData()
        {
            try
            {
                DBContext db = new DBContext();
                var result = db.ExecuteNonQuerySql("update scorelog set isdeleted = 1 where isdeleted=0;update player set average = 0;");
                return SuccessMsg("清除成功！");
            }
            catch (Exception e)
            {
                return ErrorMsg("清除失败，网络错误！");
            }
        }
    }
}
